{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "5e2b8acd",
   "metadata": {},
   "source": [
    "# mcp_exchange_rates.py server with brave search \n",
    "# This is a demo do not use for personal trading decisions\n",
    "## to get current fx and exchange rate trading recommendations\n",
    "## prequisites\n",
    "### 1. uses claude-sonnet-4\n",
    "ANTHROPIC_API_KEY in .env file\n",
    "### 2. use Brave Search API key\n",
    "BRAVE_API in .env file\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "03c51209",
   "metadata": {},
   "outputs": [],
   "source": [
    "# import libraries\n",
    "import os\n",
    "import asyncio\n",
    "from re import I\n",
    "from agents import Agent, OpenAIChatCompletionsModel, Runner, trace\n",
    "from agents.mcp import MCPServerStdio\n",
    "from openai import AsyncOpenAI\n",
    "from dotenv import load_dotenv\n",
    "from IPython.display import Markdown, display\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e5b0f282",
   "metadata": {},
   "outputs": [],
   "source": [
    "load_dotenv(override=True)\n",
    "\n",
    "# 1. Configure Claude client\n",
    "claude_client = AsyncOpenAI(\n",
    "    base_url=\"https://api.anthropic.com/v1\",\n",
    "    api_key=os.getenv(\"ANTHROPIC_API_KEY\")\n",
    ")\n",
    "\n",
    "# 2. Create Claude model\n",
    "claude_model = OpenAIChatCompletionsModel(\n",
    "    model=\"claude-sonnet-4-20250514\",\n",
    "    openai_client=claude_client\n",
    ")\n",
    "\n",
    "# 3. MCP server parameters (Brave Search)\n",
    "search_params = {\n",
    "    \"command\": \"npx\",\n",
    "    \"args\": [\"-y\", \"@modelcontextprotocol/server-brave-search\"],\n",
    "    \"env\": {\"BRAVE_API_KEY\": os.getenv(\"BRAVE_API_KEY\")}\n",
    "}\n",
    "# 4. MCP server parameters (Forex)\n",
    "exchange_rate_params = {\n",
    "    \"command\": \"uv\", \"args\": [\"run\", \"mcp_exchange_rates.py\"]\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "be86c36f",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(exchange_rate_params)\n",
    "print(search_params)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d85c78c4",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "\n",
    "try:\n",
    "    #5. Connect to MCP server\n",
    "    async with MCPServerStdio(\n",
    "        params=exchange_rate_params, client_session_timeout_seconds=30) as forex_server:\n",
    "        print(\"✅ Connected to Forex MCP server\")\n",
    "       # 6. Connect to MCP server\n",
    "        async with MCPServerStdio(\n",
    "            params=search_params, \n",
    "            client_session_timeout_seconds=60\n",
    "        ) as search_server:\n",
    "            \n",
    "            print(\"✅ Connected to Brave Search MCP server\")\n",
    "            \n",
    "            # 7. List available tools\n",
    "            tools = await search_server.list_tools()\n",
    "            forex_tools = await forex_server.list_tools()\n",
    "            tools = tools + forex_tools\n",
    "            print(f\"�� Available tools: {tools}\")\n",
    "            # 8. instructions and request\n",
    "            instructions=\"\"\"You are a financial research assistant. You have access to web search tools and usd conversion tools.\n",
    "                Use the search tools to find current information and provide accurate, up-to-date responses.\n",
    "                Always cite your sources when providing information.\"\"\"\n",
    "            \n",
    "            request =\"you have USD 100000 to investment and want to make a profit. provide the latest exchange rate of \\\n",
    "                        usd/aud usd/eur, usd/nzd, usd/gbp, usd/can - print the rates in a table format\\\n",
    "                        print the inverse rates table of the above rates\\\n",
    "                        include the source and date of the information\\\n",
    "                        for each currency pair based get the latest news about the summary of the current market conditions for each currency pair. \\\n",
    "                        provide citations for source of news/information. \\\n",
    "                        and \\\n",
    "                        the current exchange rates recommend trades for usd/aud, usd/eur, usd/nzd, usd/gbp, usd/can \\\n",
    "                        provide a trade idea based on the news and the current market conditions\"\n",
    "            # 9. Create Claude agent with MCP server\n",
    "            agent = Agent(\n",
    "                name=\"claude_researcher\",\n",
    "                instructions=instructions,\n",
    "                model=claude_model,\n",
    "                mcp_servers=[search_server,forex_server]\n",
    "            )\n",
    "            \n",
    "            print(\"✅ Claude agent created with MCP servers\")\n",
    "            \n",
    "            # 10. Use the agent with tracing\n",
    "            with trace(\"Claude and Forex Web Research\"):\n",
    "                result = await Runner.run(\n",
    "                    agent,\n",
    "                    request,\n",
    "                    max_turns=25\n",
    "                    )\n",
    "            \n",
    "            print(\"\\n📊 Claude Agent Response:\")\n",
    "            print(\"=\" * 50)\n",
    "            # print(result.final_output)\n",
    "            display(Markdown(\"This is a demo do not use for personal trading decisions\"))\n",
    "            display(Markdown(result.final_output))\n",
    "            print(\"=\" * 50)\n",
    "        \n",
    "except Exception as e:\n",
    "    print(f\"❌ Error: {e}\")\n",
    "    import traceback\n",
    "    traceback.print_exc()\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b17c96fa",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
